package com.jzsec.proxy.jzsecproxy.controller;

import com.jzsec.proxy.jzsecproxy.pojo.RequestMsg;
import com.jzsec.proxy.jzsecproxy.services.LeancloudService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import java.util.Map;

@RestController
@RequestMapping("/leancloud")
@Slf4j
public class LeanclouldController {

	@Autowired
	private LeancloudService leancloudService;

	@RequestMapping("/sendmsg")
	public Mono<String> sendMsg(@RequestBody Map msgMap) {
		String method = msgMap.get(RequestMsg.METHOD_NAME).toString();
		String url = msgMap.get(RequestMsg.URI).toString();
		Map<String, String> headerMap = null;
		Object headerObj = msgMap.get(RequestMsg.HEADER);
		if (null != headerObj) {
			headerMap = (Map) headerObj;
		}
		Map<String, Object> bodyMap = null;
		Object bodyObj = msgMap.get(RequestMsg.BODY);
		if (null != bodyObj) {
			bodyMap = (Map) bodyObj;
		}
		try {
			leancloudService.sendMessageByOkHttp(method, url, headerMap, bodyMap);
			return Mono.just("success");
		} catch (Exception e) {
			log.error(msgMap.toString(), e);
			return Mono.just(e.getMessage());
		}
	}

}
